import Vue from 'vue';
import Vuex from 'vuex';//下载并引入vuex


Vue.use(Vuex)//初始化vuex
export default new Vuex.Store({
    state:{
        num:1,
        record:[
            {
                "stock_id": 1000701,
                "title": "畅呼吸防护口罩（花粉、粉尘、PM2.5、病菌）",
                "sub_title": "五层防护、高达 98% 的平均过滤效率",
                "image": "https://resource.smartisan.com/resource/ef67ff9577c62979930b356a37b86251.jpg",
                "imgs": [
                  "https://resource.smartisan.com/resource/ef67ff9577c62979930b356a37b86251.jpg",
                  "https://resource.smartisan.com/resource/6e21fd5af69965142e1a50dca189eb7e.jpg"
                ],
                "price": 99,
                "isCheck":true,
                "snum":1,
                "limit_num": 5, //限制购买数量
                "detail": {
                  "color": "#FBF084",
                  "color_name": "绿色"
                }
            },
            {
              "stock_id": 1000801,
              "title": "坚果Pro保护膜 红色",
              "sub_title": "耐刮花、防指纹 宝石蓝",
              "image": "https://resource.smartisan.com/resource/e883f15eed51a49e1fbc9d8ddd82460b.png",
              "imgs": [],
              "price": 50,
              "isCheck":false,
              "snum":3,
              "limit_num": 5, //限制购买数量
              "detail": {
                "color": "#78282a",
                "color_name": "红色"
              }
            }
        ],
        address:[
          {
            username:'张三丰',
            phone:'13988886666',
            tel:['023','58762390'],
            loc:[
              {
                name:'北京',
                idx:'0'
              },
              {
                name:'朝阳',
                idx:'2'
              },
              {
                name:'',
                idx:'-1'
              },
            ],
            address:'3号巷子6号胡同',
            isDefault:true
          },
          {
            username:'张无忌',
            phone:'13988886665',
            tel:['023','58762394'],
            loc:[
              {
                name:'山西',
                idx:'3'
              },
              {
                name:'大同',
                idx:'1'
              },
              {
                name:'矿区',
                idx:'1'
              },
            ],
            address:'6号矿山',
            isDefault:false
          }
        ]
    },
    getters:{
      alldata(state){
        var allnum=0,allprice=0,allcheck=true
        state.record.forEach((val)=>{
          allnum+=val.snum;
          allprice+=val.snum*val.price
          if(!val.isCheck){
            allcheck=false
          }
        })
        return{
          allnum,
          allprice,
          allcheck
        }
      },
      pitchdata(state){
        var allnum=0,allprice=0,alldata=[]
        state.record.forEach((val)=>{
            if(val.isCheck){
              allnum+=val.snum;
              allprice+=val.snum*val.price
              alldata.push(val);
            }
        })
        return{
          allnum,
          allprice,
          alldata
        }
      }
    },
    mutations:{
       datamut(state,obj){
          var bool=true;
          state.record.forEach((val)=>{
                if(obj.stock_id==val.stock_id){
                  val.snum++;
                  bool=false;
                  return false
                }
          })
          if(bool){
              Vue.set(obj,"snum",1);
              Vue.set(obj,"isCheck",true);
              state.record.push(obj);
          }
           
       },
       recorddel(state,i){
          state.record.splice(i,1)
       },
       checknum(state,idx){
          state.record[idx].isCheck=!state.record[idx].isCheck
       },
       checkboolnum(state,bool){
         state.record.forEach((val)=>{
            val.isCheck=!bool
         })
       }
      
    },
  
})
